package com.javaweb.blogcite.controller;

import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.PageData;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.controller.vo.BlogItemView;
import com.javaweb.blogcite.controller.vo.BlogView;
import com.javaweb.blogcite.pojo.Blog;
import com.javaweb.blogcite.service.BlogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping
public class BlogController {
    @Resource
    private BlogService blogService;

    @GetMapping("/api/blog/{id}")
    public ResponseResult<BlogView> getBlog(@PathVariable(value="id") Integer id) {
        BlogView blogView = blogService.getBlogById(id);
        ResponseResult<BlogView> responseResult = new ResponseResult<>();
        if (blogView == null) {
            responseResult.setCode(404);
            responseResult.setMsg("博客不存在！");
            return responseResult;
        }
        responseResult.setCode(0);
        responseResult.setMsg("success");
        responseResult.setData(blogView);
        return responseResult;
    }

    @JwtCheck
    @DeleteMapping("/api/blog/{id}")
    public ResponseResult deleteBlog(@PathVariable(value="id") Integer id) {
        boolean success = blogService.deleteBlogById(id);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
            result.setData(success);
        } else {
            result.setCode(500);
            result.setMsg("删除数据失败！");
        }
        return result;
    }

    @GetMapping("/api/blog")
    public ResponseResult<PageData<BlogItemView>> getBlogsByPage(Integer page, Integer limit, Integer categoryId) {
        PageData<BlogItemView> pageData = blogService.getBlogByPage(page, limit, categoryId);
        ResponseResult<PageData<BlogItemView>> result = new ResponseResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(pageData);
        return result;
    }

    @JwtCheck
    @PutMapping("/api/blog/{id}")
    public ResponseResult updateBlog(@PathVariable(value="id") Integer id, @RequestBody Blog blog) {
        boolean success = blogService.updateBlog(id, blog);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
            result.setData(success);
        }else{
            result.setCode(500);
            result.setMsg("更新数据失败！");
        }
        return result;
    }

    @JwtCheck
    @PostMapping("/api/blog")
    public ResponseResult<Blog> publishBlog(@RequestBody Blog blog) {
        boolean success = blogService.publishBlog(blog);
        ResponseResult<Blog> result = new ResponseResult<>();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
        }else {
            result.setCode(500);
            result.setMsg("失败");
        }
        return result;
    }
}
